<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!-- 写了个公共类，把要导入的公共的文件写到一块，${ctx} -->		    
<%@include file="/WEB-INF/jsp/common/taglibs.jsp"%>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="${ctx}/lib/bootstrap-3.3.7-dist/css/bootstrap.css" />
<script type="text/javascript" src="${ctx}/lib/jquery/jquery-1.11.1.js" ></script>
<script type="text/javascript" src="${ctx}/lib/bootstrap-3.3.7-dist/js/npm.js" ></script>
<script type="text/javascript" src="${ctx}/lib/layer-v3.1.1/layer/layer.js" ></script>
<script type="text/javascript" src="${ctx}/lib/bootstrap-3.3.7-dist/js/mylayer.js" ></script>
</head>
<body>
	<form id="insert_form" action="${ctx}/student/insert.action" method="post">
		<form>
		  <div class="form-group">
		   	<label for="studentname">姓名</label>
		    <input  type="text" name="name" onblur="checkName()" class="form-control"  id="studentname" placeholder="姓名">
		  </div>
		  <div class="form-group">
		    <label for="studentage">年龄</label>
		    <input type="text" name="age" class="form-control" id="studentage" placeholder="年龄">
		  </div>
		  <div class="form-group">
		    <label for="studentgender">性别:</label>&nbsp;&nbsp;&nbsp;&nbsp;
		    <input type="radio" name="gender" value="男" checked />男 &nbsp;&nbsp;&nbsp;&nbsp;
			<input type="radio" name="gender" value="女" />女
		  </div>
		  	<!-- 选择班级 -->
		   <div class="form-group">
		    <select name="banjiId">
		    	<option>选择班级</option>
		    	<c:forEach items="${list}" var="banji">
		    		<option value="${banji.id}">${banji.name}</option>
		    	</c:forEach>
		    </select>
		  </div>
		
		  <!-- 提交ajax表单 -->
		  <button type="button" onclick="submitForm()" class="btn btn-default">添加</button>&nbsp;&nbsp;&nbsp;&nbsp;
		  <button type="reset" class="btn btn-default">重置</button>
		  
		   <!-- ajax请求，判断姓名是否重名 -->
		 	<script type="text/javascript">
		 		function checkName(){
		 			var name = document.getElementById("studentname").value;//通过本页面拿到文本框的值
				 	$.post(
				 		"${ctx}/student/checkName.action",  //url,请求服务器端的地址
				 		 {"name":name}, //传给服务器的
				 		 function(data){  //服务器给页面弹出的
				 			 if(data.isExist){
				 				mylayer.errorAlert("该学生已存在，请重新添加新学生")
				 			 }
				 		 },
				 		 "json"
				 	);
		 		}
		 	</script>
		</form>
	</form>
	
	<!--右边部分结束-->
	<script type="text/javascript">
		function gender(){
			if(id == man){
				$('input:radio:checked').val();
				
			}
		}
		
		//弹窗，提交form表单
		function submitForm(){
			$.ajax({
				url : "${ctx}/student/insert.action",
				type : "post",
				dataType : "json",
				data : $("#insert_form").serialize(), //拿到form表单内的属性值
				success : function(data){
					if(data){
						mylayer.success("添加成功");
						//关闭弹窗
						var index = parent.layer.getFrameIndex(window.name);
						setTimeout(function(){
							parent.layer.close(index);
							window.parent.location.reload();
						},1000);
					} else {
						mylayer.errorMsg("添加失败");
					}
				}
			});
		}
	</script>		
	
</body>
</html>